/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */



int sensorValue =0;
boolean sensorState[20];
boolean releState[20] ;
long lastRead[20];
long lastReadDown[20];
void setup() {
  Serial.begin(9600);
  for (int a = 0 ; a < 20; a++){
    sensorState[a] = false;
    releState[a] = 0;
    lastRead[a] = 0;
    lastReadDown[a] = 0;
    
  }
 for (int b =0; b < 20; b++){
   pinMode(b,INPUT);
   lastRead[b] = millis();
   pinMode(b+20,OUTPUT);
  lastReadDown[b] = millis();
 }
  
}
// los impares son botones, los pares reles...
void loop() {
 
 
 
  for ( int a =0 ; a < 20; a++){
    
     sensorState[a] = digitalRead(a);
  
  if (sensorState[a] == HIGH &&  ( millis() - lastRead[a] ) >300){
     if ((millis() - lastReadDown[a]) > 50 ){
       lastRead[a] = millis();
      // Serial.println("entro");
       if (releState[a] == 0 ) 
       {
          //Cambiamos de estado el rele
          if (a != 1 && a != 0 ){
                Serial.print("activado Sensor: ");
                Serial.println(a);
                releState[a] =1;
          }
          if ( a == 5  || a == 15){
             for ( int b = 2; b< 20; b++){
               releState[b] =1;
           
             
             } 
          }
       } 
       else {
          releState[a] =0;   
          if ( a == 5  || a == 15){
             for ( int b = 2; b< 20; b++){
               releState[b] =0;
           
             
             } 
          }
       }
     }
  }
  else {
    lastReadDown[a] = millis();
    
  }
  
  digitalWrite(20+a, releState[a]);
  }
}





